<template>
<view>
  <mk-goods-list :goods="goods" @clickItem="goodsItem"></mk-goods-list>
</view>
</template>

<script>
import {mainService} from "../../../api/buyer/main";
import {attachmentService} from "../../../api/buyer/public/attachmentService";
import {BASE_IMG_URL} from "../../../api/service";

export default {
  name: "index",
  data(){
    return{
      goods:[],
    }
  },
  created(){
    this.init()
  },
  methods:{
    init(){
      const queryParams = {}
      queryParams.currentPage = 1
      queryParams.pageSize = 100
      queryParams.orderBys=[
        {
          property:"createDatetime",
          order:"desc"
        }
      ]

      let end=new Date().getTime()
      let start=end-604800000;
      queryParams.filters={
        createDatetime:start+"$,$"+end
      }
      mainService.findCommoditys(queryParams).then((res)=> {
        console.log(res)
        // debugger
        // cons
        let resData = res.data.datas;
        if (resData && resData.length != 0) {
          resData.forEach((res => {
            // console.log(res,BASE_URL,res.attachments.length!=0?res.attachments[0].attachmentAddr:"")
            let good = {};
            good.id = res.eid
            good.name = "Commodity"
            good.img =""
            good.title = res.description
            good.littleTitle = res.shopName
            good.amount = res.minPrice
            good.amountRemark = (res.saleNum?res.saleNum:0)+" 已购买"
            // good.lable = "包邮"
            good.commodity = res
            this.goods.push(good)
          }))
          this.loadImage()
        }

      })
    },

    loadImage(){
      //懒加载图片
      this.goods.forEach(good => {
        if (!good.img||good.img==="") {
          if (good.id && good.name) {
            attachmentService.findFile(good.id, good.name).then((res) => {
              good.img = BASE_IMG_URL + res.data[0].attachmentAddr
            })
          }
        }
      })
    },
    goodsItem(item){
      console.log(item)
      uni.navigateTo({
        url: '/pages/buyer/main/goodDetailed?good=' + encodeURIComponent(JSON.stringify(item.commodity)),
        // url: '/pages/staff/application/production/taskList/task-detail'
      })
    },
  }
}
</script>

<style scoped>

</style>